<template>
	<el-dialog v-model="visible" :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" draggable>
  <el-form ref="dataFormRef" :model="dataForm" :rules="dataRules" :label-width="100">
  <el-form-item label="调度费订单" prop="dispatchOrderId">
      <el-input v-model="dataForm.dispatchOrderId" placeholder="调度费订单号"></el-input>
  </el-form-item>
  <el-form-item label="订单状态" prop="dispatchOrderStatus">
	<el-select v-model="dataForm.dispatchOrderStatus" placeholder="请选择支付状态">
        <el-option label="未支付" value="0"></el-option>
        <el-option label="已支付" value="1"></el-option>
      </el-select>
  </el-form-item>
  <el-form-item label="订单金额" prop="dispatchOrderCost">
      <el-input v-model="dataForm.dispatchOrderCost" placeholder="调度费订单金额"></el-input>
  </el-form-item>
  <el-form-item label="微信支付号" prop="dispatchTransactionId">
      <el-input v-model="dataForm.dispatchTransactionId" placeholder="调度费订单对应微信支付号"></el-input>
  </el-form-item>
  <el-form-item label="付款时间" prop="payTime">
      <el-input v-model="dataForm.payTime" placeholder="付款时间"></el-input>
  </el-form-item>
  <el-form-item label="用户微信id" prop="wxOpenId">
      <el-input v-model="dataForm.wxOpenId" placeholder="用户微信id"></el-input>
  </el-form-item>
  <el-form-item label="设备id" prop="deviceId">
      <el-input v-model="dataForm.deviceId" placeholder="设备id"></el-input>
  </el-form-item>
  <el-form-item label="设备名称" prop="deviceName">
      <el-input v-model="dataForm.deviceName" placeholder="设备名称"></el-input>
  </el-form-item>
  </el-form>
		<template #footer>
			<el-button @click="visible = false">取消</el-button>
			<el-button type="primary" @click="submitHandle()">确定</el-button>
		</template>
	</el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { ElMessage } from 'element-plus/es'
import { useOrderDispatchOrderApi, useOrderDispatchOrderSubmitApi } from '@/api/order/dispatchOrder'

const emit = defineEmits(['refreshDataList'])

const visible = defineModel<boolean>('visible')
const dataFormRef = ref()

const dataForm = reactive({
	id: '',
	dispatchOrderId: '',
	dispatchOrderStatus: '',
	dispatchOrderCost: '',
	dispatchTransactionId: '',
	creator: '',
	createTime: '',
	updater: '',
	updateTime: '',
	deleted: '',
	payTime: '',
	wxOpenId: '',
	deviceId: '',
	deviceName: ''})

const init = (id?: number) => {
	if (id) {
		getDispatchOrder(id)
	}
}

const getDispatchOrder = (id: number) => {
	useOrderDispatchOrderApi(id).then(res => {
		Object.assign(dataForm, res.data)
	})
}

const dataRules = ref({
	dispatchOrderId: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	dispatchOrderStatus: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	dispatchOrderCost: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	dispatchTransactionId: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	payTime: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	wxOpenId: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	deviceId: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	deviceName: [{ required: true, message: '必填项不能为空', trigger: 'blur' }]})

// 表单提交
const submitHandle = () => {
	dataFormRef.value.validate(async (valid: boolean) => {
		if (!valid) {
			return false
		}


		useOrderDispatchOrderSubmitApi(dataForm).then(() => {
			ElMessage.success({
				message: '操作成功',
				duration: 500,
				onClose: () => {
					visible.value = false
					emit('refreshDataList')
				}
			})
		})
	})
}

defineExpose({
	init
})
</script>
